/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is features.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 *
 **************************/

function getFeatures(objXmlDocument) {
  
    /***********************************************
    * Function Name(s): getFeatures
    *
    * Purpose:
    * Gets the Oister XML file, uses a For loop to 
    * read each of the features (including child elements)
    * one feature at a time.  Calls the writeFeaturesSetup
    * and writeFeature functions for each feature, then 
    * moves on to the next feature until all features
    * have been read.
    ************************************************/
    var intCounter = 0;
    var intFeaturesLength = 0;
    var objAuthorEmail = "";       
    var objContributors = "";  
    var objComments = "";   
    var objCopyright = "";         
    var objDocumentation = "";     
    var objFeature = "";           
    var objFeatures = "";          
    var objFeatureAuthor = "";    
    var objFeatureChildren = ""; 
    var objFeatureContact = "";    
    var objFeatureDesc = "";      
    var objFeatureHide = "";     
    var objFeatureLink = "";       
    var objFeatureMap = "";      
    var objFeaturePriority = "";   
    var objFeatureTags = "";      
    var objFeatureTitle = "";      
    var objGUID = "";   
    var objImage = "";
    var objImageCount = 0;
    var objImageOne = "";
    var objImageTwo = "";
    var objImageThree = ""; 
    var objMedia = ""; 
    var objShowUntil = "";
    var objTimestamp = "";         
    var strAuthorEmail = "";      
    var strContributors = "";     
    var strComments = ""; 
    var strCopyright = "";
    var strDocumentation = "";
    var strFeatureAuthor = "";     
    var strFeatureContact = "";    
    var strFeatureDesc = "";  
    var strFeatureHide = "";     
    var strFeatureLink = "";       
    var strFeatureMap = "";  
    var strFeaturePriority = "lowest";      
    var strFeatureTags = "";       
    var strFeatureTitle = "";      
    var strGUID = "";
    var strImageOneCaption = "";
    var strImageOneLink = "";
    var strImageThreeCaption = "";
    var strImageThreeLink = "";
    var strImageTwoCaption = "";
    var strImageTwoLink = "";
    var strMediaFilesize = "";
    var strMediaLink = "";
    var strMediaType = "";
    var strShowUntil = "";
    var strTimestamp = "";
  
    objFeatures = objXmlDocument.getElementsByTagName("feature");
    //Total number of feature elements.
    intFeaturesLength = objFeatures.length; 
  
    //Verify there's at least one feature in the Oister XML
    if (intFeaturesLength > 0) {
        //Use loop to grab everything from the features, one feature at a time.
        for (intCounter = 0; intCounter < intFeaturesLength; intCounter++) {
            //Get the first feature that hasn't been processed yet.
            objFeature = objFeatures[intCounter];
            objFeaturePriority = objFeature.getAttribute("priority");
            if (objFeaturePriority !== undefined) {
                strFeaturePriority = objFeaturePriority;
            }
            objFeatureHide = objFeature.getAttribute("hidepriority");
            //Check to see whether the "hidepriority" attribute" exists.
            if (objFeatureHide !== null && objFeatureHide !== undefined) {
                //If "hidepriority" isn't set, give it default value of false
                objFeature.setAttribute("hidepriority", "false");
            }         
            //Check for the feature's title, Oister Schema requires it.
            objFeatureTitle = objFeature.getElementsByTagName("title")[0];
            if (objFeatureTitle !== null && objFeatureTitle !== undefined) {
			          //Get the actual value of the feature's title.
                strFeatureTitle = objFeatureTitle.firstChild.nodeValue;
            } 
            //Check to see if the feature has a description element.
            objFeatureDesc = objFeature.getElementsByTagName("description")[0];
            if (objFeatureDesc !== null && objFeatureDesc !== undefined &&
                    objFeatureDesc.firstChild.nodeValue !== "" &&
                    objFeatureDesc.firstChild.nodeValue !== "<![CDATA[]]>") {
                //Get the value of the current feature's description.  
                strFeatureDesc = objFeatureDesc.firstChild.nodeValue;	
            } 
		        //Get any contact information.
		        objFeatureContact = objFeature.getElementsByTagName("contact")[0];
		        if (objFeatureContact !== null && objFeatureContact !== undefined) {
		            strFeatureContact = objFeatureContact.firstChild.nodeValue;
	          } 
		        //Get the link to the feature 
            objFeatureLink = objFeature.getElementsByTagName("featurelink")[0];
		        if (objFeatureLink !== null && objFeatureLink !== undefined) {
                strFeatureLink = objFeatureLink.firstChild.nodeValue;
            }
		        //Get any tags
		        objFeatureTags = objFeature.getElementsByTagName("tags")[0];
		        if (objFeatureTags !== null && objFeatureTags !== undefined) {
		            strFeatureTags = objFeatureTags.firstChild.nodeValue;
            }
            //Get any map info
		        objFeatureMap = objFeature.getElementsByTagName("map")[0];
            if (objFeatureMap !== null && objFeatureMap !== undefined) {
                strFeatureMap = objFeatureMap.firstChild.nodeValue;
            }
			      //Get the name of the feature's author
			      objFeatureAuthor = objFeature.getElementsByTagName("author")[0];
            if (objFeatureAuthor !== null && objFeatureAuthor !== undefined) {
                strFeatureAuthor = objFeatureAuthor.firstChild.nodeValue;
            }
		        //Get the e-mail address of the feature's author
		        objAuthorEmail = objFeature.getElementsByTagName("authorEmail")[0];
		        if (objAuthorEmail !== null && objAuthorEmail !== undefined) {
                strAuthorEmail = objAuthorEmail.firstChild.nodeValue;
            }
            //Get the info of any contributors
            objContributors = objFeature.getElementsByTagName("contributors")[0];
            if (objContributors !== null && objContributors !== undefined) {
                strContributors = objContributors.firstChild.nodeValue;
            }
            //Get comments info
            objComments = objFeature.getElementsByTagName("comments")[0];
            if (objComments !== null && objComments !== undefined) {
                strComments = objComments.firstChild.nodeValue;
            }
            //Get any copyright info
            objCopyright = objFeature.getElementsByTagName("copyright")[0];
            if (objCopyright !== null && objCopyright !== undefined) {
                strCopyright = objCopyright.firstChild.nodeValue;
            }
            //Get any extra documentation
            objDocumentation = objFeature.getElementsByTagName("documentation")[0];
            if (objDocumentation !== null && objDocumentation !== undefined) {
                strDocumentation = objDocumentation.firstChild.nodeValue;
            }
            //Get any GUID that might be set
            objGUID = objFeature.getElementsByTagName("guid")[0];
            if (objGUID !== null && objGUID !== undefined) {
                strGUID = objGUID.firstChild.nodeValue;
            }
            //Get the feature's timestamp if it exists
            objTimestamp = objFeature.getElementsByTagName("timestamp")[0];
            if (objTimestamp !== null && objTimestamp !== undefined) {
                strTimestamp = objTimestamp.firstChild.nodeValue;
            }
            //Get the feature's showuntil datetime if it exists
            objShowUntil = objFeature.getElementsByTagName("showuntil")[0];
            if (objShowUntil !== null && objShowUntil !== undefined) {
                strShowUntil = objShowUntil.firstChild.nodeValue;
            }
            //Check to see if there are any images defined
            objImage = objFeature.getElementsByTagName("image");
            objImageCount = objImage.length;
            if (objImageCount !== undefined && objImageCount > 0) {
                //Get the first image, since there's at least one.
				        objImageOne = objImage[0];
					      //Check to see if the imglink and caption attributes are set
                strImageOneLink = objImageOne.getAttribute("imglink");
                strImageOneCaption = objImageOne.getAttribute("caption");
            
                //Check to see if there's a second image
                objImageTwo = objImage[1];
					      //Grab the imglink and caption attributes if so 
                if (objImageTwo !== undefined && objImageTwo !==null) {
                    strImageTwoLink = objImageTwo.getAttribute("imglink");
                    strImageTwoCaption = objImageTwo.getAttribute("caption");
                }
                //Check to see if there's a third image.
                objImageThree = objImage[2];
                //Grab the imglink and caption attributes if so. 
                if (objImageThree !== undefined && objImageThree !==null) {
                    strImageThreeLink = objImageThree.getAttribute("imglink");
                    strImageThreeCaption = objImageThree.getAttribute("caption");
                }
            }
            //Check to see if there's a media element in the feature.
            objMedia = objFeature.getElementsByTagName("media")[0];
            if (objMedia !== null && objMedia !== undefined) {
                //Grab all the related values: type, filesize, filelink.
                strMediaType = objMedia.getAttribute("type");
                strMediaFilesize = objMedia.getAttribute("filesize");
                strMediaLink = objMedia.getAttribute("filelink");
            }
		
	
    //Create an array that contains all the variables that were defined above.  
    objFeatureChildren = [strFeatureTitle, strFeatureDesc, objImageOne, 
                            strImageOneLink, strImageOneCaption, objImageTwo,
                            strImageTwoLink, strImageTwoCaption, objImageThree,
                            strImageThreeLink, strImageThreeCaption, 
                            strFeatureContact, strFeatureLink, strFeatureTags,
                            strFeatureMap,strFeatureAuthor, strAuthorEmail, 
                            strContributors, strComments, strCopyright, strDocumentation,
                            strGUID, strTimestamp, strShowUntil, strMediaType,
                            strMediaFilesize, strMediaLink];
			
    //Call the writeFeature function and pass the appropriate values.
  	writeFeature(intCounter, strFeaturePriority, strFeatureHide, objFeatureChildren);

		    }
    }
}	
function writeFeature(intCounter, strFeaturePriority, strFeatureHide, 
    objFeatureChildren) {
    /***********************************************
    * Function Name(s): writeFeature
    *
    * Purpose:
    * Modifies the DOM of the HTML document that's presented
    * to the user, places the feature according to the 
    * feature's priority level or hides the feature if hidepriority
    * was set.  If feature's child elements have non-empty values, those 
    * are displayed as well. Also calls the checkShowUntil function to 
    * see if any set showuntil datetime has passed and if checkShowUntil
    * function returns true, the feature is not displayed.
    ************************************************/
    var dblMediaKilobytes = (parseFloat(objFeatureChildren[24]))/1024;
    var dblMediaMegabytes = dblMediaKilobytes/1024;
    var dblMediaSizeDisplayed = 0;      
    var objDivAuthor = "";  
    var objDivAuthorEmail = "";
    var objDivBreak = "";
    var objDivContact = "";
    var objDivContributors = "";
    var objDivComments = "";
    var objDivCopyright = "";
    var objDivDesc = "";
    var objDivDocumentation = "";
    var objDivFeature = "";
    var objDivGUID = "";
    var objDivImageOne = "";
    var objDivImageThree = "";
    var objDivImageTwo = "";
    var objDivLink = "";
    var objDivMap = "";
    var objDivPriority = "";
    var objDivTitle = "";
    var objImageOne = objFeatureChildren[2];
    var objImageThree = objFeatureChildren[8];
    var objImageTwo = objFeatureChildren[5];
    var objMediaHTML5 = "";
    var strAuthorEmail = objFeatureChildren[16];
    var strCapitalizePriority = "";
    var strContributors = objFeatureChildren[17];
    var strComments = objFeatureChildren[18];
    var strCommentsHTML = "";
    var strCopyright = objFeatureChildren[19];
    var strDivFeatureId = "";
    var strDocumentation = objFeatureChildren[20];
    var strFeatureAuthor = objFeatureChildren[15];
    var strFeatureContact = objFeatureChildren[11];
    var strFeatureDesc = objFeatureChildren[1];
    var strFeatureIdNum = "";
    var strFeatureLink = objFeatureChildren[12];
    var strFeatureMap = objFeatureChildren[14];
    var strFeatureTags = objFeatureChildren[13];
    var strFeatureTitle = objFeatureChildren[0];
    var strGUID = objFeatureChildren[21];
    var strImageOneCaption = objFeatureChildren[4];
    var strImageOneLink = objFeatureChildren[3];
    var strImageThreeCaption = objFeatureChildren[10];
    var strImageThreeLink = objFeatureChildren[9];
    var strImageTwoCaption = objFeatureChildren[7];
    var strImageTwoLink = objFeatureChildren[6];
    var strMediaFilesize = objFeatureChildren[25];
    var strMediaFormatAudioPattern = /\.(mp3|wav|ogg)$/i;
    var strMediaFormatVideoPattern = /\.(flv|mov|mp4|m4v|ogv|webm|avi|wmv)$/i;
    var strMediaFormat = "";
    var strMediaLink = objFeatureChildren[26];
    var strMediaType = objFeatureChildren[24];
    var strPermalinkHTML = "";
    var strShowUntil = objFeatureChildren[23];
    var strTimestamp = objFeatureChildren[22];
 
    //A unique ID for the feature node that will be created.
    strFeatureIdNum = "feature"+intCounter;
    //Capitalize the first letter of the priority level.
    strCapitalizePriority = strFeaturePriority.charAt(0).toUpperCase()+
        strFeaturePriority.substring(1);
	
    //Create a new node in the DOM that will contain the feature, 
    //place it according to priority level.
    objDivFeature = document.createElement("div");
    objDivFeatureId = objDivFeature.id = strFeatureIdNum;
    objDivFeature.className = "feature" + strCapitalizePriority;
    document.getElementById(strFeaturePriority).appendChild(objDivFeature);
		
		//Create a node for title.
    objDivTitle = document.createElement("div");
    objDivTitle.id = "title" + intCounter;
	  objDivTitle.className = "featureTitle";
    objDivFeature.appendChild(objDivTitle);
    objDivTitle.innerHTML = strFeatureTitle;
    
    //Create a node for the author of the feature, if it the author was defined
    if (strFeatureAuthor !== "") {
        objDivAuthor = document.createElement("div");
        objDivAuthor.id = "author" + intCounter;
        objDivAuthor.className = "featureAuthor";
        objDivFeature.appendChild(objDivAuthor);
        objDivAuthor.innerHTML = "By " + strFeatureAuthor;
    }
			
    //create a node fo the author's e-mail, if it was defined
    if (strAuthorEmail !== ""){
		    objDivAuthorEmail = document.createElement("div");
        objDivAuthorEmail.id = "authorEmail" + intCounter;
        objDivAuthorEmail.className = "featureAuthorEmail";
        objDivAuthor.appendChild(objDivAuthorEmail);
        objDivAuthorEmail.innerHTML = "Author's Email: " + strAuthorEmail;
    }
			
    //create a node to show the priority level, unless "hidepriority" was set to true
    if (strFeatureHide !== "true") {
        objDivPriority = document.createElement("div");
        objDivPriority.id = "priority" + intCounter;
        objDivPriority.className = "priority";
        objDivFeature.appendChild(objDivPriority);
        objDivPriority.innerHTML = "Priority: " + strCapitalizePriority;
    }
			
    //check for any images and create the nodes accordingly
    if (objImageOne !== "") {
        objDivImageOne = document.createElement("div");
		    objDivImageOne.id = "div" + strFeatureIdNum + "imageOne";
		    objDivImageOne.className = "featureImageDiv";
		    objDivFeature.appendChild(objDivImageOne);
        objDivImageOne.innerHTML = "<img class=\"featureImage\" id=\"";
        obvDivImageOne.innerHTML += strFeatureIdNum;
        objDivImageOne.innerHTML += "imageOne\" src=\"" + strImageOneLink;
        objDivImageOne.innerHTML += "\" alt=" + strImageOneCaption + "\">";
		 }
		 if (objImageTwo !== "") {
        objDivImageTwo = document.createElement("div");
        objDivImageTwo.id = "div" + strFeatureIdNum + "imageTwo";
        objDivImageTwo.className = "featureImageDiv";
        objDivFeature.appendChild(objDivImageTwo);
        objDivImageTwo.innerHTML = "<img class=\"featureImage\" id=\"";
        obvDivImageTwo.innerHTML += strFeatureIdNum;
        objDivImageTwo.innerHTML += "imageTwo\" src=\"" + strImageTwoLink;
        objDivImageTwo.innerHTML += "\" alt=" + strImageTwoCaption + "\">";
     }
     if (objImageThree !== "") {
        objDivImageThree = document.createElement("div");
        objDivImageThree.id = "div" + strFeatureIdNum + "imageThree";
        objDivImageThree.className = "featureImageDiv";
        objDivFeature.appendChild(objDivImageThree);
        objDivImageThree.innerHTML = "<img class=\"featureImage\" id=\"";
        obvDivImageThree.innerHTML += strFeatureIdNum;
        objDivImageThree.innerHTML += "imageThree\" src=\"" + strImageThreeLink;
        objDivImageThree.innerHTML += "\" alt=" + strImageThreeCaption + "\">";
     }
    //Create a node for the feature's description
    objDivDesc = document.createElement("div");
    objDivDesc.id = "description" + intCounter;
    objDivDesc.className = "description";
    objDivFeature.appendChild(objDivDesc);
    objDivDesc.innerHTML = strFeatureDesc;
    //Create a node for any contact info, if the contact info was defined
    if (strFeatureContact !== "") {
		    objDivContact = document.createElement("div");
	      objDivContact.id = "contact" + intCounter;
        objDivContact.className = "featureContact";
        objDivFeature.appendChild(objDivContact);
        objDivContact.innerHTML = "Contact: " + strFeatureContact;
    }
    //Create a node for the link to the feature, if it exists
    if (strFeatureLink !== "") {
        objDivLink = document.createElement("div");
	      objDivLink.id = "featurelink" + intCounter;
        objDivLink.className = "featurelink";
        objDivFeature.appendChild(objDivLink);
        strPermalinkHTML = "Permalink: <a href=\"" + strFeatureLink + "\">";
        strPermalinkHTML += strFeatureLink + "</a>";
        objDivLink.innerHTML = strPermalinkHTML;
    }	
    //Create a node for any contributor info, if the info was defined.
    if (strContributors !== "") {
        objDivContributors = document.createElement("div");
        objDivContributors.id = "contributors" + intCounter;
        objDivContributors.className = "contributors";
        objDivFeature.appendChild(objDivContributors);
        objDivContributors.innerHTML = "Additional Information Provided By ";
        objDivContributors.innerHTML += strContributors;
    }		
    //Create a node any comments info, if comments were defined.
    if (strComments !== "") {
        objDivComments = document.createElement("div");
        objDivComments.id = "comments" + intCounter;
        objDivComments.className = "comments";
        objDivFeature.appendChild(objDivComments);
        strCommentsHTML = "Comments: <a href=\"" + strComments + "\">";
        strCommentsHTML += strComments + "</a>";
        objDivComments.innerHTML = strCommentsHTML;
    }
    //Create a node for any tags, if tags were defined
    if (strFeatureTags !== "") {
        objDivTags = document.createElement("div");
        objDivTags.id = "tags" + intCounter;
        objDivTags.className = "tags";
        objDivFeature.appendChild(objDivTags);
        objDivTags.innerHTML = "Tags: " + strFeatureTags;
    }
    //Create a node for a link to any map, if map was defined.
    if (strFeatureMap !== "") {
        objDivMap = document.createElement("div");
        objDivMap.id = "map" + intCounter;
        objDivMap.className = "map";
        objDivFeature.appendChild(objDivMap);
        objDivMap.innerHTML = "Map: <a href=\"" + strFeatureMap +"\">";
        objDivMap.innerHTML += strFeatureMap + "</a>";
    }
		//Create a node for any copyright info, if copyright info was defined
    if (strCopyright !== "") {
        objDivCopyright = document.createElement("div");
        objDivCopyright.id = "copyright" + intCounter;
        objDivCopyright.className = "copyright";
        objDivFeature.appendChild(objDivCopyright);
        objDivCopyright.innerHTML = strCopyright;
    }
		//Create a node for any documentation, if it was defined
    if (strDocumentation !== "") {
        objDivDocumentation = document.createElement("div");
        objDivDocumentation.id = "documentation" + intCounter;
        objDivDocumentation.className = "documentation";
        objDivFeature.appendChild(objDivDocumentation);
        objDivDocumentation.innerHTML = strDocumentation;
    }
    //Create a node for any GUID, if it was defined
    if (strGUID !== "") {
        objDivGUID = document.createElement("div");
        objDivGUID.id = "guid" + intCounter;
        objDivGUID.className = "guid";
        objDivFeature.appendChild(objDivGUID);
        objDivGUID.innerHTML = strGUID;
    }  
		//Create a node for the feature's timestamp, if it was defined
    if (strTimestamp !== "") {
        objDivTimestamp = document.createElement("div");
        objDivTimestamp.id = "timestamp" + intCounter;
        objDivTimestamp.className = "timestamp";
        objDivFeature.appendChild(objDivTimestamp);
        objDivTimestamp.innerHTML = "Created: " + strTimestamp;
    }
    //Create a node for the ShowUntil datetime, if it was defined.
    if (strShowUntil !== "") {
        objDivShowuntil = document.createElement("div");
        objDivShowuntil.id = "featureshowuntil" + intCounter;
        objDivShowuntil.className = "featureShowUntil";
        objDivFeature.appendChild(divFeatureShowuntil);
        objDivShowuntil.innerHTML = "Show Until: " + strShowUntil;
    }
    //Call checkShowUntil to see if showUntil datetime has been reached
		hideFeature = function() {
		              checkShowUntil(strShowUntil);
                }
		//If showUntil datetime has been reached, don't display the feature
		if (hideFeature === true) {
        objDivFeature.style.display = "none";  
		}
    //Create a node for any media that was defined
    if (strMediaLink !== "") {
        objDivMedia = document.createElement("div");
        objDivMedia.id = "media" + intCounter;
        objDivMedia.className = "media";
        objDivFeature.appendChild(objDivMedia);
        if (strMediaFormatVideoPattern.test(strMediaFiletype)) {
            //File is a video, so use HTML5 video tag
            objHTML5Video = document.createElement("video");
            objHTML5Video.id = "video" + intCounter;
            objHTML5Video.className = "video";
            objDivMedia.appendChild(objHTML5Video);
            objHTML5Video.setAttribute("src", strMediaLink);
            objHTML5Video.setAttribute("controls");
            objHTML5Video.setAttribute("preload");
            objHTML5Video.setAttribute("width","320");
            objHTML5Video.setAttribute("height","240");
        }
        else if (strMediaFormatAudioPattern.test(strMediaFiletype)) {
            //File is audio, so use HTML5 audio tag
            objHTML5Audio = document.createElement("audio");
            objHTML5Audio.id = "audio" + intCounter;
            objHTML5Audio.className = "audio";
            objDivMedia.appendChild(objHTML5Audio);
            objHTML5Audio.setAttribute("src", strMediaLink);
            objHTML5Audio.setAttribute("controls");
            objHTML5Audio.setAttribute("preload");
        }
        //File is neither video or audio (or not a supported format)
        else {
            //If the filesize is less than 0.5 MB, show the size in KB)
            if (dblMediaMegabytes < 0.5) {
                strMediaSizeDisplayed = Math.round(dblMediaKilobytes);
                strMediaSizeDisplayed = (strMediaSizeDisplayed * 100) / 100;
                strMediaSizeDisplayed = strMediaSizeDisplayed + " KB";
            }
            //If the filesize is bigger 0.5 MB or larger, show size in MB)
            else {
                strMediaSizeDisplayed = Math.round(dblMediaMegabytes *100);
                strMediaSizeDisplayed = strMediaSizeDisplayed / 100 + " MB";
            }
            //HTML5 tags aren't going to work, so fall back to a standard link
            objDivMedia.innerHTML = "Media Link: <a href=\"" + strMediaLink;
            objDivMedia.innerHTML += "\">" + strMediaLink + "</a><br/>";
            //Show the file type and filesize so for user's benefit
            objDivMedia.innerHTML += "Media Type: " + strMediaType + "<br/>";
            objDivMedia.innerHTML += "Media Size: " + strMediaSizeDisplayed;
        }
    }
}